{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Tomo extended"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Import modules"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Basic stuff\n",
    "import sys\n",
    "sys.path.append(\"/net/server/homes/sep/gbarnier/code/gpu/acousticIsoOp/test/lib/python/\")\n",
    "import genericIO\n",
    "import SepVector\n",
    "import Hypercube\n",
    "import Acoustic_iso_double\n",
    "import numpy as np\n",
    "import time\n",
    "\n",
    "# Plotting library\n",
    "import matplotlib.pyplot as plt\n",
    "import sepPlot"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Wavelet (sources)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "!/net/server/homes/sep/gbarnier/code/gpu/acousticIsoOp/test/bin/waveletMain.py timeDelay=1.0 f1=2 f2=5 f3=8 f4=15 par=parTomoExt.p type=ali wavelet=waveletTomoExt.H"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Velocity models"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " \n",
      "------------------------ Model padding program --------------------\n",
      "Original nz = 350 [samples]\n",
      "Original nx = 1700 [samples]\n",
      " \n",
      "zPadMinus = 100 [samples]\n",
      "zPadPlus = 110 [samples]\n",
      "xPadMinus = 100 [samples]\n",
      "xPadPlus = 104 [samples]\n",
      " \n",
      "blockSize = 16 [samples]\n",
      "FAT = 5 [samples]\n",
      " \n",
      "New nz = 570 [samples including padding and FAT]\n",
      "New nx = 1914 [samples including padding and FAT]\n",
      "-------------------------------------------------------------------\n",
      " \n",
      " \n",
      "------------------------ Model padding program --------------------\n",
      "Original nz = 350 [samples]\n",
      "Original nx = 1700 [samples]\n",
      " \n",
      "zPadMinus = 100 [samples]\n",
      "zPadPlus = 110 [samples]\n",
      "xPadMinus = 100 [samples]\n",
      "xPadPlus = 104 [samples]\n",
      " \n",
      "blockSize = 16 [samples]\n",
      "FAT = 5 [samples]\n",
      " \n",
      "New nz = 570 [samples including padding and FAT]\n",
      "New nx = 1914 [samples including padding and FAT]\n",
      "-------------------------------------------------------------------\n",
      " \n"
     ]
    }
   ],
   "source": [
    "# True velocity model\n",
    "!/net/server/homes/sep/gbarnier/code/gpu/acousticIsoOp/test/bin/padFileGpuMain zPad=100 xPad=100 model=velocityMarmousi.H data=velocityMarmousi.pad.H\n",
    "\n",
    "# Background velocity model\n",
    "!Smooth rect1=10 rect2=10 < velocityMarmousi.H > velocityMarmousiSmooth.H\n",
    "!/net/server/homes/sep/gbarnier/code/gpu/acousticIsoOp/test/bin/padFileGpuMain zPad=100 xPad=100 model=velocityMarmousiSmooth.H data=velocityMarmousiSmooth.pad.H"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Extended reflectivity"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " \r\n",
      "------------------------ Model padding program --------------------\r\n",
      "Original nz = 350 [samples]\r\n",
      "Original nx = 1700 [samples]\r\n",
      " \r\n",
      "zPadMinus = 100 [samples]\r\n",
      "zPadPlus = 110 [samples]\r\n",
      "xPadMinus = 100 [samples]\r\n",
      "xPadPlus = 104 [samples]\r\n",
      " \r\n",
      "blockSize = 16 [samples]\r\n",
      "FAT = 5 [samples]\r\n",
      " \r\n",
      "New nz = 570 [samples including padding and FAT]\r\n",
      "New nx = 1914 [samples including padding and FAT]\r\n",
      "-------------------------------------------------------------------\r\n",
      " \r\n"
     ]
    }
   ],
   "source": [
    "# Create an extended reflectivity\n",
    "!Spike n1=350 n2=1700 d1=1.0 d2=1.0 n3=1 d3=1.0 velback=0.0 nsp=1 mag=0.01 k1=250 k2=850 > junk.H\n",
    "!Pad beg3=5 end3=15 extend=0 < junk.H > tomoOffsetReflectivity.H\n",
    "!echo \"d1=0.01 d2=0.01 d3=0.01 o1=0.0 o2=0.0 o3=-0.1\" >> tomoOffsetReflectivity.H\n",
    "\n",
    "# Pad extended reflectivity\n",
    "!/net/server/homes/sep/gbarnier/code/gpu/acousticIsoOp/test/bin/padFileGpuMain zPad=100 xPad=100 model=tomoOffsetReflectivity.H data=tomoOffsetReflectivity.pad.H"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Model (background perturbation)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " \r\n",
      "------------------------ Model padding program --------------------\r\n",
      "Original nz = 350 [samples]\r\n",
      "Original nx = 1700 [samples]\r\n",
      " \r\n",
      "zPadMinus = 100 [samples]\r\n",
      "zPadPlus = 110 [samples]\r\n",
      "xPadMinus = 100 [samples]\r\n",
      "xPadPlus = 104 [samples]\r\n",
      " \r\n",
      "blockSize = 16 [samples]\r\n",
      "FAT = 5 [samples]\r\n",
      " \r\n",
      "New nz = 570 [samples including padding and FAT]\r\n",
      "New nx = 1914 [samples including padding and FAT]\r\n",
      "-------------------------------------------------------------------\r\n",
      " \r\n"
     ]
    }
   ],
   "source": [
    "# Background perturbation\n",
    "!Spike n1=350 n2=1700 d1=1.0 d2=1.0 n3=1 d3=1.0 velback=0.0 nsp=1 mag=0.01 k1=250 k2=850 > tomoModel.H\n",
    "\n",
    "# Pad reflectivity model\n",
    "!/net/server/homes/sep/gbarnier/code/gpu/acousticIsoOp/test/bin/padFileGpuMain zPad=100 xPad=100 model=tomoModel.H data=tomoModel.pad.H"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Initialize tomo extended operator"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "nts=1000\r\n",
      "dts=0.008\r\n",
      "sub=8\r\n",
      "nz=570\r\n",
      "nx=1914\r\n",
      "zPadMinus=100\r\n",
      "zPadPlus=110\r\n",
      "xPadMinus=100\r\n",
      "xPadPlus=104\r\n",
      "dz=0.01\r\n",
      "dx=0.01\r\n",
      "fMax=20\r\n",
      "zSource=2\r\n",
      "xSource=850\r\n",
      "nShot=1\r\n",
      "spacingShots=10\r\n",
      "depthReceiver=2\r\n",
      "nReceiver=1700\r\n",
      "dReceiver=1\r\n",
      "oReceiver=1\r\n",
      "blockSize=16\r\n",
      "fat=5\r\n",
      "nGpu=8\r\n"
     ]
    }
   ],
   "source": [
    "args=[\"dummy arg\",\"vel=velocityMarmousiSmooth.pad.H\",\"sources=waveletTomoExt.H\",\"par=parTomoExt.p\",\"model=tomoModel.pad.H\",\"reflectivity=tomoOffsetReflectivity.pad.H\",\"extension=offset\",\"nExt=21\"]\n",
    "modelDouble,dataDouble,velDouble,parObject,sourcesVector,sourcesSignalsVector,receiversVector,reflectivitydouble=Acoustic_iso_double.tomoExtOpInitDouble(args)\n",
    "\n",
    "# Printing parameter file for reference\n",
    "!cat parTomoExt.p"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Create tomo object"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "tomoExtOp=Acoustic_iso_double.tomoExtShotsGpu(modelDouble,dataDouble,velDouble,parObject,sourcesVector,sourcesSignalsVector,receiversVector,reflectivitydouble)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Read model (background perturbation)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "modelFile=parObject.getString(\"model\")\n",
    "modelFloat=genericIO.defaultIO.getVector(modelFile,ndims=2)\n",
    "modelDouble=SepVector.getSepVector(modelFloat.getHyper(),storage=\"dataDouble\")\n",
    "modelDMat=modelDouble.getNdArray()\n",
    "modelSMat=modelFloat.getNdArray()\n",
    "modelDMat[:]=modelSMat"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Tomo extended forward"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "tomoExtOp.forward(False,modelDouble,dataDouble)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
